#include <iostream>
#include <math.h>
#include "skill_func.h"
#include "Monster.h"
#include "Player.h"
#include "SkillTable.h"
#include "FightFormule.h"
#include "mapobj_manager.h"
#include "entity.h"

CDamageAction40::CDamageAction40( )
{
}

CDamageAction40::~CDamageAction40( )
{
}

bool CDamageAction40::ActionEffect(uint32_t skillid, CEntity& src, CEntity& dec)
{
	SkillInfo * _si = SkillTable::GetInstance().GetSkillInfoById(skillid);
	if (nullptr == _si)
	{
		return false;
	}

	//	获取场景的战斗公式
	FightFormule _fight_formule;

	//	取得双方战斗属性
	FightProperty& _att_fp = src.GetFightPro();
	FightProperty& _def_fp = dec.GetFightPro();

	//	取得双方详细属性
	double _def_aromr = _att_fp.GetProperty(property_type::armor);
	int32_t _att_level = int32_t(_att_fp.GetProperty(property_type::level));

	double _def_armor_redurce_damage_probability =
		_fight_formule.CaculateProbabilityArmorDamageReduction(_att_level, _def_aromr);


	int32_t _skill_level = _si->GetLevel();
	double _base = _si->GetBaseVal();
	double _level_grow = _si->GetGrowVal();
	double _skill_coe = _si->GetCOE();
	double _att_val = _att_fp.GetProperty(Int16ToPropertyType(_si->GetAttributeId() - 1));
	double _skill_base_value = _fight_formule.CaculateSkillBaseValue(_skill_level, _base, _level_grow, _skill_coe, _att_val);
	double _damage =_fight_formule.CaculateSkillPhysicsDamage(_skill_base_value, _def_armor_redurce_damage_probability);


	if (dec.GetEntityType() == OBJTYPE_CREATURE)
	{
		Monster& _mst = dynamic_cast<Monster&>(dec);
		_mst.ModifyHp(src.GetKey(), (int32_t)_damage);
	}

	if (OBJTYPE_PLAYER == dec.GetEntityType())
	{
		Player& _player = dynamic_cast<Player&>(dec);
		//LOG_ERROR("default", "damage[%f]", _damage);
		_player.ModifyHp(src.GetKey(), dec.GetKey(), (int32_t)_damage <= 0 ? 1 : (int32_t)_damage);
	}

	return true;
}

bool CDamageAction40::ActionEffect(uint32_t skill_id, uint32_t src_key, uint32_t des_key)
{
	CEntity *src = CMAPObjManager::GetInstance().GetObjectByID<CEntity>(src_key);
	CEntity *des = CMAPObjManager::GetInstance().GetObjectByID<CEntity>(des_key);
	if (src == NULL || des == NULL) {
		return false;
	}
	return CDamageAction40::ActionEffect(1, *src, *des);
}

bool CDamageAction40::ActionEffectForMonster(CEntity& entity, uint32_t src_key,int32_t blood)
{
	Monster& monster = dynamic_cast<Monster&>(entity);

	monster.ModifyHp(src_key, blood);
	return true;
}